home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test21.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  19KB  |  651 lines

  1. /* POSIX test program (21).            Author: Andy Tanenbaum */
  2.  
  3. /* The following POSIX calls are tested:
  4.  *
  5.  *    rename(),  mkdir(),  rmdir()
  6.  */
  7.  
  8.  
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <limits.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15.  
  16. #define ITERATIONS        10
  17. #define MAX_ERROR 4
  18.  
  19. int subtest, errct;
  20. extern errno;
  21.  
  22. main(argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.  
  27.   int i, m = 0xFFFF;
  28.  
  29.   sync();
  30.   if (geteuid() == 0) {
  31.     /* Must not run as root. */
  32.     setgid(2);
  33.     setuid(2);
  34.   }
  35.  
  36.   if (argc == 2) m = atoi(argv[1]);
  37.   printf("Test 21 ");
  38.  
  39.   for (i = 0; i < ITERATIONS; i++) {
  40.     if (m & 00001) test21a();
  41.     if (m & 00002) test21b();
  42.     if (m & 00004) test21c();
  43.     if (m & 00010) test21d();
  44.     if (m & 00020) test21e();
  45.     if (m & 00040) test21f();
  46.     if (m & 01000) test21g();
  47.     if (m & 00200) test21h();
  48.     if (m & 00400) test21i();
  49.     if (m & 01000) test21k();
  50.     if (m & 02000) test21l();
  51.     if (m & 04000) test21m();
  52.     if (m & 010000) test21n();
  53.   }
  54.   if (errct == 0)
  55.     printf("ok\n");
  56.   else
  57.     printf(" %d errors\n", errct);
  58.   exit(0);
  59. }
  60.  
  61. test21a()
  62. {
  63. /* Test rename(). */
  64.  
  65.   int fd, fd2, fd3;
  66.   char buf[PATH_MAX+1], buf1[PATH_MAX+1], buf2[PATH_MAX+1];
  67.   struct stat stat1, stat2;
  68.   extern char *getcwd();
  69.  
  70.   subtest = 1;
  71.  
  72.   unlink("A1");            /* get rid of it if it exists */
  73.   unlink("A2");            /* get rid of it if it exists */
  74.   unlink("A3");            /* get rid of it if it exists */
  75.   unlink("A4");            /* get rid of it if it exists */
  76.   unlink("A5");            /* get rid of it if it exists */
  77.   unlink("A6");            /* get rid of it if it exists */
  78.   unlink("A7");            /* get rid of it if it exists */
  79.  
  80.   /* Basic test.  Rename A1 to A2 and then A2 to A3. */
  81.   if ( (fd=creat("A1", 0666)) < 0) e(1);
  82.   if (write(fd, buf, 20) != 20) e(2);
  83.   if (close(fd) < 0) e(3);
  84.   if (rename("A1", "A2") < 0) e(4);
  85.   if ( (fd=open("A2", O_RDONLY)) < 0) e(5);
  86.   if (rename("A2", "A3") < 0) e(6);
  87.   if ( (fd2=open("A3", O_RDONLY)) < 0) e(7);
  88.   if (close(fd) != 0) e(8);
  89.   if (close(fd2) != 0) e(9);
  90.   if (unlink("A3") != 0) e(10);
  91.  
  92.   /* Now get the absolute path name of the current directory using getcwd()
  93.    * and use it to test RENAME using different combinations of relative and
  94.    * absolute path names.
  95.    */
  96.   if (getcwd(buf, PATH_MAX) == (char *) NULL) e(11);
  97.   if ( (fd = creat("A4", 0666)) < 0) e(12);
  98.   if (write(fd, buf, 30) != 30) e(13);
  99.   if (close(fd) != 0) e(14);
  100.   strcpy(buf1, buf);
  101.   strcat(buf1, "/A4");
  102.   if (rename(buf1, "A5") != 0) e(15);    /* rename(absolute, relative) */
  103.   if (access("A5", 6) != 0) e(16);    /* use access to see if file exists */
  104.   strcpy(buf2, buf);
  105.   strcat(buf2, "/A6");
  106.   if (rename("A5", buf2) != 0) e(17);    /* rename(relative, absolute) */
  107.   if (access("A6", 6) != 0) e(18);
  108.   if (access(buf2, 6) != 0) e(19);
  109.   strcpy(buf1, buf);
  110.   strcat(buf1, "/A6");
  111.   strcpy(buf2, buf);
  112.   strcat(buf2, "/A7");
  113.   if (rename(buf1, buf2) != 0) e(20);    /* rename(absolute, absolute) */
  114.   if (access("A7", 6) != 0) e(21);
  115.   if (access(buf2, 6) != 0) e(22);
  116.  
  117.   /* Try renaming using names like "./A8" */
  118.   if (rename("A7", "./A8") != 0) e(23);
  119.   if (access("A8", 6) != 0) e(24);
  120.   if (rename("./A8", "A9") != 0) e(25);
  121.   if (access("A9", 6) != 0) e(26);
  122.   if (rename("./A9", "./A10") != 0) e(27);
  123.   if (access("A10", 6) != 0) e(28);
  124.   if (access("./A10", 6) != 0) e(29);
  125.   if (unlink("A10") != 0) e(30);
  126.  
  127.   /* Now see if directories can be renamed. */
  128.   if (system("rm -rf ?uzzy scsi") != 0) e(31);
  129.   if (system("mkdir fuzzy") != 0) e(32);
  130.   if (rename("fuzzy", "wuzzy") != 0) e(33);
  131.   if ( (fd=creat("wuzzy/was_a_bear", 0666)) < 0) e(34);
  132.   if (access("wuzzy/was_a_bear", 6) != 0) e(35);
  133.   if (unlink("wuzzy/was_a_bear") != 0) e(36);
  134.   if (close(fd) != 0) e(37);
  135.   if (rename("wuzzy", "buzzy") != 0) e(38);
  136.   if (system("rmdir buzzy") != 0) e(39);
  137.  
  138.   /* Now start testing the case that 'new' exists. */
  139.   if ( (fd = creat("horse", 0666)) < 0) e(40);
  140.   if ( (fd2 = creat("sheep", 0666)) < 0) e(41);
  141.   if (write(fd, buf, PATH_MAX) != PATH_MAX) e(42);
  142.   if (write(fd2, buf, 23) != 23) e(43);
  143.   if (stat("horse", &stat1) != 0) e(44);
  144.   if (rename("horse", "sheep") != 0) e(45);
  145.   if (stat("sheep", &stat2) != 0) e(46);
  146.   if (stat1.st_dev != stat2.st_dev) e(47);
  147.   if (stat1.st_ino != stat2.st_ino) e(48);
  148.   if (stat2.st_size != PATH_MAX) e(49);
  149.   if (access("horse", 6) == 0) e(50);
  150.   if (close(fd) != 0) e(51);
  151.   if (close(fd2) != 0) e(52);
  152.   if (rename("sheep", "sheep") != 0) e(53);
  153.   if (unlink("sheep") != 0) e(54);
  154.  
  155.   /* Now try renaming something to a directory that already exists. */
  156.   if (system("mkdir fuzzy wuzzy") != 0) e(55);
  157.   if ( (fd = creat("fuzzy/was_a_bear", 0666)) < 0) e(56);
  158.   if (close(fd) != 0) e(57);
  159.   if (rename("fuzzy", "wuzzy") != 0) e(58);    /* 'new' is empty dir */
  160.   if (system("mkdir scsi") != 0) e(59);
  161.   if (rename("scsi", "wuzzy") == 0) e(60);    /* 'new' is full dir */
  162.   if (errno != EEXIST && errno != ENOTEMPTY) e(61);
  163.  
  164.   /* Test 14 character names--always tricky. */
  165.   if (rename("wuzzy/was_a_bear", "wuzzy/was_not_a_bear") != 0) e(62);
  166.   if (access("wuzzy/was_not_a_bear", 6) != 0) e(63);
  167.   if (rename("wuzzy/was_not_a_bear", "wuzzy/was_not_a_duck") != 0) e(64);
  168.   if (access("wuzzy/was_not_a_duck", 6) != 0) e(65);
  169.   if (rename("wuzzy/was_not_a_duck", "wuzzy/was_a_bird") != 0) e(65);
  170.   if (access("wuzzy/was_a_bird", 6) != 0) e(66);
  171.  
  172.   /* Test moves between directories. */
  173.   if (rename("wuzzy/was_a_bird", "beast") != 0) e(67);
  174.   if (access("beast", 6) != 0) e(68);
  175.   if (rename("beast", "wuzzy/was_a_cat") != 0) e(69);
  176.   if (access("wuzzy/was_a_cat", 6) != 0) e(70);
  177.  
  178.   /* Test error conditions. 'scsi' and 'wuzzy/was_a_cat' exist now. */
  179.   if (rename("wuzzy/was_a_cat", "wuzzy/was_a_dog") != 0) e(71);
  180.   if (access("wuzzy/was_a_dog", 6) != 0) e(72);
  181.   if (chmod("wuzzy", 0) != 0) e(73);
  182.  
  183.   errno = 0;
  184.   if (rename("wuzzy/was_a_dog", "wuzzy/was_a_pig") != -1) e(74);
  185.   if (errno != EACCES) e(75);
  186.  
  187.   errno = 0;
  188.   if (rename("wuzzy/was_a_dog", "doggie") != -1) e(76);
  189.   if (errno != EACCES) e(77);
  190.  
  191.   errno = 0;
  192.   if ( (fd = creat("beast", 0666)) < 0) e(78);
  193.   if (close(fd) != 0) e(79);
  194.   if (rename("beast", "wuzzy/was_a_twit") != -1) e(80);
  195.   if (errno != EACCES) e(81);
  196.  
  197.   errno = 0;
  198.   if (rename("beast", "wuzzy") != -1) e(82);
  199.   if (errno != EISDIR) e(83);
  200.  
  201.   errno = 0;
  202.   if (rename("beest", "baste") != -1) e(84);
  203.   if (errno != ENOENT) e(85);
  204.  
  205.   errno = 0;
  206.   if (rename("wuzzy", "beast") != -1) e(86);
  207.   if (errno != ENOTDIR) e(87);
  208.  
  209.   /* Test prefix rule. */
  210.   errno = 0;
  211.   if (chmod("wuzzy", 0777) != 0) e(88);
  212.   if (unlink("wuzzy/was_a_dog") != 0) e(89);
  213.   strcpy(buf1, buf);
  214.   strcat(buf1, "/wuzzy");
  215.   if (rename(buf, buf1) != -1) e(90);
  216.   if (errno != EINVAL) e(91);
  217.  
  218.   if (system("rm -rf wuzzy beast scsi") != 0) e(92);
  219. }
  220.  
  221.   
  222.  
  223. test21b()
  224. {
  225. /* Test mkdir() and rmdir(). */
  226.  
  227.   int i;
  228.   char name[3];
  229.   struct stat statbuf;
  230.  
  231.   subtest = 2;
  232.  
  233.   /* Simple stuff. */
  234.   if (mkdir("D1", 0700) != 0) e(1);
  235.   if (stat("D1", &statbuf) != 0) e(2);
  236.   if (!S_ISDIR(statbuf.st_mode)) e(3);
  237.   if ( (statbuf.st_mode & 0777) != 0700) e(4);
  238.   if (rmdir("D1") != 0) e(5);
  239.  
  240.   /* Make and remove 40 directories.  By doing so, the directory has to
  241.    * grow to 2 blocks.  That presents plenty of opportunity for bugs.
  242.    */
  243.   name[0] = 'D';
  244.   name[2] = '\0';
  245.   for (i = 0; i < 40; i++) {
  246.     name[1] = 'A' + i;
  247.     if (mkdir(name, 0700 + i%7) != 0) e(10+i);    /* for simplicity */
  248.   }
  249.   for (i = 0; i < 40; i++) {
  250.     name[1] = 'A' + i;
  251.     if (rmdir(name) != 0) e(50+i);
  252.   }
  253. }
  254.  
  255. test21c()
  256. {
  257. /* Test mkdir() and rmdir(). */
  258.  
  259.   int i;
  260.   char name[3];
  261.   struct stat statbuf;
  262.  
  263.   subtest = 3;
  264.  
  265.   if (mkdir("D1", 0777) != 0) e(1);
  266.   if (mkdir("D1/D2", 0777) != 0) e(2);
  267.   if (mkdir("D1/D2/D3", 0777) != 0) e(3);
  268.   if (mkdir("D1/D2/D3/D4", 0777) != 0) e(4);
  269.   if (mkdir("D1/D2/D3/D4/D5", 0777) != 0) e(5);
  270.   if (mkdir("D1/D2/D3/D4/D5/D6", 0777) != 0) e(6);
  271.   if (mkdir("D1/D2/D3/D4/D5/D6/D7", 0777) != 0) e(7);
  272.   if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8", 0777) != 0) e(8);
  273.   if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8/D9", 0777) != 0) e(9);
  274.   if (access("D1/D2/D3/D4/D5/D6/D7/D8/D9", 7) != 0) e(10);
  275.   if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8/D9") != 0) e(11);
  276.   if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8") != 0) e(12);
  277.   if (rmdir("D1/D2/D3/D4/D5/D6/D7") != 0) e(13);
  278.   if (rmdir("D1/D2/D3/D4/D5/D6") != 0) e(11);
  279.